package com.supor.aiot.mine.ui.wechat;

import android.content.Intent;
import android.view.View;

import com.android.baseconfig.common.utils.ToastUtils;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.base.BaseMVVMActivity;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.databinding.ActivityWeChatBinding;

public class WeChatActivity extends BaseMVVMActivity<WechatViewModel, ActivityWeChatBinding> {

    @Override
    protected int resourceId() {
        return R.layout.activity_we_chat;
    }

    @Override
    protected void initView() {
        super.initView();
        bindingView.setViewM(this);
        bindingView.toolbar.setIvLeftOnClickListener(v -> finish());
        bindingView.toolbar.setTvLeftOnClickListener(v -> finish());
        bindingView.btnStart.setOnClickListener(v -> viewModel.setWechatOauth("yes"));
        bindingView.btnClose.setOnClickListener(v -> {
            showLoading();
            viewModel.setWechatOauth("no");
        });
    }

    @Override
    protected void initData() {
        super.initData();
        showLoading();
        if (!AppAgent.isLogin()) {
            ToastUtils.getInstance().showCustomToast("请先登录账号才能使用微信授权");
            bindingView.llBtn.setVisibility(View.GONE);
            closeLoading();
            return;
        }
        // 获取用户微信授权开关状态
        viewModel.getWechatOauthStatus();
        viewModel.getOauthStatusResult().observe(this, result -> {
            closeLoading();
            if (result instanceof Result.Success) {
                String data = (String) result.getData();
                if ("yes".equalsIgnoreCase(data)) {
                    bindingView.btnStart.setVisibility(View.GONE);
                    bindingView.btnClose.setVisibility(View.VISIBLE);
                } else {
                    bindingView.btnStart.setVisibility(View.VISIBLE);
                    bindingView.btnClose.setVisibility(View.GONE);
                }
            } else if (result instanceof Result.Error) {
                ToastUtils.getInstance().showCustomToast("获取微信授权信息失败: " + ((Result.Error) result).getErrorMsg());
            }
        });

        viewModel.getOauthResult().observe(this, result -> {
            closeLoading();
            if (result instanceof Result.Success) {
                String data = (String) result.getData();
                if ("no".equalsIgnoreCase(data)) {
                    bindingView.btnStart.setVisibility(View.VISIBLE);
                    bindingView.btnClose.setVisibility(View.GONE);
                    ToastUtils.getInstance().showCustomToast("关闭微信服务成功");
                } else if ("yes".equalsIgnoreCase(data)) {
                    bindingView.btnStart.setVisibility(View.GONE);
                    bindingView.btnClose.setVisibility(View.VISIBLE);
                    ToastUtils.getInstance().showCustomToast("开启微信服务成功");
                } else {
                    startActivity(new Intent(this, WechatStartActivity.class));
                }
            } else if (result instanceof Result.Error) {
                ToastUtils.getInstance().showCustomToast("关闭微信服务失败: " + ((Result.Error) result).getErrorMsg());
            }
        });
    }
}